﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace OEMS
{
    public static class OverbrengingDB
    {
        public static List<Overbrenging> GetOverbrengingen()
        {
            List<Overbrenging> overbrengingList = new List<Overbrenging>();
            SqlConnection connection = OemsDB.GetConnection();
            string selectStatement =
                "SELECT Type, Datum_ingebruikname, GegevensID  " +
                "FROM Gegevens_Overbrenging";
            SqlCommand selectCommand = new SqlCommand(selectStatement, connection);

            try
            {
                connection.Open();
                SqlDataReader reader = selectCommand.ExecuteReader();
                while (reader.Read())
                {
                    Overbrenging overbrenging = new Overbrenging();
                    overbrenging.Type = reader["Type"].ToString();
                    overbrenging.Ingebruikname = (DateTime)reader["Datum_ingebruikname"];
                    overbrenging.GegevensId = (int)reader["GegevensID"];
                    overbrengingList.Add(overbrenging);
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }

            return overbrengingList;
        }

        public static Overbrenging GetOverbrengingFromId(int gegevensId)
        {
            Overbrenging overbrenging = new Overbrenging();
            SqlConnection connection = OemsDB.GetConnection();
            string selectStatement =
                "SELECT Type, Datum_ingebruikname, GegevensID " +
                "FROM Gegevens_Overbrenging " + 
                "WHERE GegevensID = " + gegevensId;
            SqlCommand selectCommand = new SqlCommand(selectStatement, connection);

            try
            {
                connection.Open();
                SqlDataReader reader = selectCommand.ExecuteReader();
                while (reader.Read())
                {
                    overbrenging.Type = reader["Type"].ToString();
                    overbrenging.Ingebruikname = (DateTime)reader["Datum_ingebruikname"];
                    overbrenging.GegevensId = (int)reader["GegevensID"];
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }

            return overbrenging;
        }
    }
}
